package cn.coldleaf.wiki.controllers;

import cn.coldleaf.wiki.pojo.dto.DocQueryDto;
import cn.coldleaf.wiki.pojo.dto.DocSaveDto;
import cn.coldleaf.wiki.pojo.vo.DocListVo;
import cn.coldleaf.wiki.pojo.vo.DocVo;
import cn.coldleaf.wiki.pojo.vo.ResponseVo;
import cn.coldleaf.wiki.services.DocService;
import cn.coldleaf.wiki.utils.ErrorCode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文档Controller
 * @datetime 2023/10/20 上午11:01
 * @user cold
 **/
@Slf4j
@RestController
@RequestMapping("doc")
@Api(tags = "文档操作接口")
@AllArgsConstructor
public class DocController {
    private final DocService docService;

    /**
     * 创建文档
     * @datetime 2023-10-20 12:50:23
     * @user cold
     **/
    @Operation(summary = "创建文档")
    @PostMapping("save")
    public ResponseVo<?> saveDoc(@RequestBody DocSaveDto dto){
        DocListVo vo = docService.saveDoc(dto);
        if (vo != null){
            return ResponseVo.success(vo);
        }
        return ResponseVo.error(ErrorCode.DOC_SAVE_ERROR);
    }

    /**
     * 修改文档
     * @datetime 2023-10-20 12:52:00
     * @user cold
     **/
    @Operation(summary = "修改文档")
    @PostMapping("modify")
    public ResponseVo<?> modifyDoc(@RequestBody DocSaveDto dto){
        DocListVo vo = docService.modifyDoc(dto);
        if (vo != null){
            return ResponseVo.success(vo);
        }
        return ResponseVo.error(ErrorCode.DOC_MODIFY_ERROR);
    }

    /**
     * 删除文档
     * @datetime 2023-10-20 12:53:12
     * @user cold
     **/
    @Operation(summary = "删除文档")
    @PostMapping("remove")
    public ResponseVo<?> removeDoc(@RequestBody DocSaveDto dto){
        int ret = docService.removeDoc(dto);
        if (ret > 0){
            return ResponseVo.success(null);
        }
        return ResponseVo.error(ErrorCode.DOC_REMOVE_ERROR);
    }

    /**
     * 搜索文档
     * @datetime 2023-10-20 12:55:36
     * @user cold
     **/
    @Operation(summary = "搜索文档")
    @GetMapping("search")
    public ResponseVo<Page<DocListVo>> searchList(DocQueryDto dto){
        Page<DocListVo> page = docService.searchList(dto);
        return ResponseVo.success(page);
    }

    /**
     * 获取文档列表(不包含文档内容)
     * @datetime 2023-10-20 12:56:26
     * @user cold
     **/
    @Operation(summary = "获取文档列表(不包含文档内容)")
    @GetMapping("list")
    public ResponseVo<List<DocListVo>> getList(DocQueryDto dto){
        List<DocListVo> vos = docService.getList(dto);
        return ResponseVo.success(vos);
    }

    /**
     * 获取指定文档
     * @datetime 2023-10-20 12:58:59
     * @user cold
     **/
    @Operation(summary = "获取指定文档")
    @GetMapping("get")
    public ResponseVo<DocVo> getDoc(DocQueryDto dto){
        DocVo vo = docService.getDoc(dto);
        if (vo != null){
            return ResponseVo.success(vo);
        }
        return ResponseVo.error(ErrorCode.DOC_NOT_EXISTS_ERROR);
    }

}
